package cn.tedu.lbs.basic.web.rpc;

import cn.tedu.lbs.basic.api.BasicApi;
import cn.tedu.lbs.basic.web.service.AdminService;
import cn.tedu.lbs.basic.web.service.UserGroupService;
import cn.tedu.lbs.basic.web.service.UserService;
import com.tarena.lbs.pojo.basic.dto.AdminDTO;
import com.tarena.lbs.pojo.basic.dto.UserDTO;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@DubboService
@Component
@Slf4j
public class BasicApiImpl implements BasicApi {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserGroupService userGroupService;
    /**
     * 关于rpc调用接口实现 遵循以下一些约定
     * 1.不对外抛出 业务异常
     * 2.返回值体现成功失败 true false 如果是对象 null 非null
     */
    @Override
    public AdminDTO getAdminDetail(Integer adminId) {
        AdminDTO adminDTO=null;
        try{
            AdminVO adminVO = adminService.detail(adminId);
            adminDTO=new AdminDTO();
            BeanUtils.copyProperties(adminVO,adminDTO);
        }catch (Exception e){
            log.error("获取商家管理员详情异常",e);
        }
        return adminDTO;
    }

    @Override
    public List<Integer> getUserGroupIds(Integer userId, Integer businessId) {
        //利用userId 查询关联的标签集合 再查询关联的 用户人群
        List<Integer> groupIds=null;
        try{
            groupIds=userGroupService.getUserGroupIds(userId,businessId);
        }catch (Exception e){
            log.error("获取用户人群异常",e);
        }
        return groupIds;
    }

    @Override
    public UserDTO getUserDetail(Integer userId) {
        UserDTO dto=null;
        try{
            dto=userService.detail(userId);
        }catch (Exception e){
            log.error("获取用户详情异常",e);
        }
        return dto;
    }
}
